package com.g2room.tools.mybatisTool;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.g2room.tools.db.db2po.DataSourceModel;
import com.g2room.tools.db.db2po.DbConstant;
import com.g2room.tools.db.db2po.DbUtil;
import com.g2room.tools.db.db2po.StringUtil;
import com.g2room.tools.mybatisTool.to.ClazzTo;
import com.g2room.tools.mybatisTool.to.Field;

public class TableInfo {
	private static String importDate = "java.util.Date";
	private static String importBigDecimal = "java.math.BigDecimal";
	// 包名
	private static String basePkg = "com.carry6.self.";
	// 数据库表前缀，在生成Java代码时需要去掉
	private static String suff = "";
	
	public static void main(String[] args) {
		List<ClazzTo> list = getTableInfo();
		for(ClazzTo clazz : list) {
			System.out.println(clazz.getPoName().toString());
		}
	}
	
	public static DbUtil getDbUtil() {
		DataSourceModel ds = new DataSourceModel();
		ds.setDriver("com.mysql.jdbc.Driver");
		ds.setUrl("jdbc:mysql://10.238.253.9:3306/esports_refact?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&autoReconnectForPools=true");
		ds.setUsername("root");
		ds.setPassword("root");
		return new DbUtil(ds);
	}
	/**
	 * 生成java文件
	 *
	 * @auther <a href="mailto:g2room@163.com">天涯</a>
	 * Jan 9, 2014 5:08:05 PM
	 */
	public static List<ClazzTo> getTableInfo() {
		DbUtil dbUtil = getDbUtil();
		Map<String, String> tables = dbUtil.getTablesByStatus();
		
		boolean isComment = true;
		List<ClazzTo> clazzToList = new ArrayList<ClazzTo>();
		for (String tableName : tables.keySet()) {
			ClazzTo clazzTo = new ClazzTo();
			clazzTo.setPoSerialVersionUID(StringUtil.genRandomNum(5) + System.nanoTime());
			List<Field> fields = new ArrayList<Field>();
			String minTn = StringUtil.hump4Underline(tableName);
			
			minTn = StringUtil.hump4Underline(tableName.substring(suff.length(), tableName.length()));
			
			String[] tableNames = new String[]{"lol_hero","dota2_hero","player_dota2","team_dota2", "team", "one_competition_dota2", "one_competition", 
					"one_competition_dota2_player", "one_competition_lol_player","one_competition_lol_team","one_competition_dota2_team","one_competition_event_dota2",
					"one_competition_event","competition_event","competition_event_dota2","competition_event_player_dota2","competition_event_player",
					"competition_event_team_dota2","competition_event_team","competition_dota2","competition","game_ban_dota2","game_ban",};
			List<String> list = new ArrayList<String>();
			list.add("lol_hero");
			list.add("dota2_hero");
			list.add("player_dota2");
			list.add("team_dota2");
			list.add("team");
			list.add("one_competition_dota2");
			list.add("one_competition");
			list.add("one_competition_dota2_player");
			list.add("one_competition_lol_player");
			list.add("one_competition_lol_team");
			list.add("one_competition_dota2_team");
			list.add("one_competition_event_dota2");
			list.add("one_competition_event");
			list.add("competition_event");
			list.add("competition_event_dota2");
			list.add("competition_event_player_dota2");
			list.add("competition_event_player");
			list.add("competition_event_team_dota2");
			list.add("competition_event_team");
			list.add("competition_dota2");
			list.add("competition");
			list.add("game_ban_dota2");
			list.add("game_ban");
			if(!list.contains(tableName)) {
				continue;
			}
//			if(tableName.startsWith(suff)){
//				minTn = StringUtil.hump4Underline(tableName.substring(suff.length(), tableName.length()));
//			} else {
//				continue;
//			}
			String tableRemark = null;
			if(!StringUtil.isBlank(tables.get(tableName))) {
				tableRemark = "/** " + tables.get(tableName) + " **/\n";
			}
			clazzTo.setRemark(tableRemark);
			// 此处写死ID
			clazzTo.setIdField("#{id}");
			String capTn = StringUtil.capitalize(minTn);
			clazzTo.setAlias(minTn);
			clazzTo.setBasePkg(basePkg);
			clazzTo.setPoName(capTn);
			clazzTo.setMapName(capTn + "Map");
			clazzTo.setServiceName(capTn + "Service");
			clazzTo.setTableName(tableName);
			clazzTo.setXmlName(capTn);
			clazzTo.setMapperName(capTn + "Mapper");
			
			Map<String, String> colns = dbUtil.getColumns(tableName, isComment);
			String column = "";
			List<String> importPkgs = new ArrayList<String>();
			for (String key : colns.keySet()) {
				Field fieldTo = new  Field();
				if(isComment) {
					String comm = key.substring(key.indexOf(" ") + 1);
					if(!StringUtil.isBlank(comm)) {
						fieldTo.setRemark("/* " + comm + " */");
					}
					column = key.substring(0, key.indexOf(" "));
				} else {
					column = key;
				}
				String fn = StringUtil.hump4Underline(column);
				String javaType;
				if(column.equalsIgnoreCase("id")) {
					javaType = DbConstant.FIELD_TYPE_LONG;
				} else {
					javaType = colns.get(key);
				}
				fieldTo.setColumn(column);
				fieldTo.setFieldName(fn);
				fields.add(fieldTo);
				
				fieldTo.setJavaType(javaType);
				
				if (javaType.equals(DbConstant.FIELD_TYPE_DATE) && !importPkgs.contains(importDate)) {
					importPkgs.add(importDate);
				}
				if (javaType.equals(DbConstant.FIELD_TYPE_BIGDECIMAL) && !importPkgs.contains(importBigDecimal)) {
					importPkgs.add(importBigDecimal);
				}
			}
			Collections.sort(fields);
			clazzTo.setFields(fields);
			clazzTo.setImportPkgs(importPkgs);
			clazzToList.add(clazzTo);
		}
		// 进行排序处理
		Collections.sort(clazzToList);
		return clazzToList;
	}

}
